package com.ruoyi.api.zero;

import com.fasterxml.jackson.core.type.TypeReference;
import com.ruoyi.project.projectInfo.domain.ProjectInfo;
import com.ruoyi.project.projectInfo.service.IProjectInfoService;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.util.JacksonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

@RequestMapping("/api/zeroProject")
@RestController
public class ZeroProjectController {
  @Autowired
  private IProjectInfoService projectInfoService;
  @Autowired
  private ISysUserService userService;

  /**
   * 给项目配置项目负责人
   */
  @PostMapping("/setProjectLeader")
  public void  setProjectLeader(@RequestParam Long projectId, @RequestParam Long userId){
    SysUser sysUser = userService.selectUserById(userId);
    if(sysUser !=null){
      ProjectInfo projectInfo =
        projectInfoService.selectProjectInfoById(projectId);
      if(projectInfo !=null){
        projectInfo.setLeaderId(userId);
        projectInfo.setLeaderName(sysUser.getUserName());
        projectInfoService.updateProjectInfo(projectInfo);
      }
    }
  }

  @PostMapping("/projectInfoJsonImport")
  public  void projectInfoJsonImport(){
    BufferedReader bufferedReader=null;
    ClassPathResource fileSystemResource=new ClassPathResource("project_info.json");
    try {
      InputStreamReader isr = new InputStreamReader(fileSystemResource.getInputStream());
      bufferedReader=new BufferedReader(isr);
      String line=null;
      StringBuilder sb=new StringBuilder();
      while ((line=bufferedReader.readLine())!=null){

        sb.append(line);
      }
      String s = sb.toString();
      List<ProjectInfo> list = JacksonUtil.json2Bean(s, new TypeReference<List<ProjectInfo>>() {
      });

      System.out.println(list.size());
      for(ProjectInfo projectInfo:list){
        projectInfoService.insertProjectInfo(projectInfo);
      }
    }catch (Exception e){
      e.printStackTrace();
    }finally {
      try{
        bufferedReader.close();
      }catch (Exception e){}

    }
  }
}
